package top.techqi.hamtools.binder

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.item_diary.view.*
import top.techqi.droidbdf.widget.recycler.RecyclerBinder
import top.techqi.hamtools.R
import top.techqi.hamtools.db.assist.RstT
import top.techqi.hamtools.db.model.DiaryModel
import java.text.SimpleDateFormat
import java.util.*

class DiaryBinder(context: Context) : RecyclerBinder<DiaryModel, DiaryBinder.ViewHolder>(context) {
    override fun onCreateViewHolder(inflater: LayoutInflater, parent: ViewGroup): ViewHolder {
        return ViewHolder(inflater.inflate(R.layout.item_diary, parent, false))
    }

    override fun onBindViewHolder(holder: ViewHolder, item: DiaryModel) {
        holder.itemView.sign.text = item.sign
        holder.itemView.datetime.text = SDF.format(item.datetime ?: 0)
        holder.itemView.r_value.text = "${item.rmtR.value}"
        holder.itemView.s_value.text = "${item.rmtS.value}"
        holder.itemView.t_value.text = "${item.rmtT.value}"
        holder.itemView.t_layout.visibility = if (item.rmtT == RstT.UNKNOWN) View.GONE else View.VISIBLE
        holder.itemView.mode.text = item.mode
        holder.itemView.freq.text = context.getString(R.string.item_diary_freq_format, item.freq, item.freqUnit.name)
        holder.itemView.power.text = context.getString(R.string.item_diary_power_format, item.power)
    }

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view)

    companion object {
        val SDF = SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.ENGLISH)
    }
}